[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Helper.RemovalButton.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Helper.RemovalButton.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Items;
    using BM.Tools.WebControls.MForm.Generator;
    using BM.Tools.WebControls.MForm.ScriptReferences;

    /// <summary>
    /// The control that displays a button, that removes a corresponding item on client side on click
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm", "BM.Tools.WebControls.MForm.Controls.Helper.RemovalButton.js")]
    public class RemovalButton : WebControl
    {        
        private readonly OffspringItem owner;
        
        private string onclickFunction = "BM.RemovalButton.run(this.parentNode);return false;";

        /// <summary>
        /// Initializes a new instance of the <see cref="RemovalButton"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public RemovalButton(OffspringItem owner)
        {
            this.owner = owner;
            CssClass = "MForm-Btn MForm-CloseBtn";
        }

        /// <summary>
        /// Gets or sets OnclickFunction.
        /// </summary>
        /// <value>
        /// The onclick function.
        /// </value>
        public virtual string OnclickFunction
        {
            get
            {
                return onclickFunction;
            }

            set
            {
                onclickFunction = value;
            }
        }

        /// <summary>
        /// Gets or sets Visible.
        /// </summary>
        /// <value>
        /// The visible.
        /// </value>
        public override bool Visible
        {
            get
            {
                if (owner.Mode == FormBuilderMode.View)
                {
                    return false;
                }

                if (owner.RemovalDisabled)
                {
                    return false;
                }

                if (!owner.Basis.HasVariableOccurs)
                {
                    return false;
                }

                return base.Visible;
            }

            set
            {
                base.Visible = value;
            }
        }

        /// <summary>
        /// Gets TagKey.
        /// </summary>
        /// <value>
        /// The tag key.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.A;
            }
        }

        /// <summary>
        /// Adds the needed controls attributes
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");                        
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, OnclickFunction);            
        }

        /// <summary>
        /// Includes associated embedded resources
        /// </summary>
        /// <param name="e">
        /// The event args.
        /// </param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ScriptIncluder.IncludeResources(this);
        }
    }
}